package com.loanpcase;

import com.alibaba.fastjson.JSONObject;
import com.util.HttpSend;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public abstract class LPCase {

    protected static String TEST_URL = Config.TEST_CONTEXT_URL_1603;

    public void run(String sp){

        String url = null;
        if(sp!=null){
            url = sp+getRequestUrl();
        }else{
            url = String.format("%s%s:%s/%s%s",Config.POC,Config.SERVER_IP,Config.SERVER_PORT,Config.APP_NAME,getRequestUrl());
        }
        System.out.println("URL:"+url);
        try {
            String result = null;
            String requ = JSONObject.toJSONString(getRequestMap());
            System.out.println(requ);
            if(url.toUpperCase().startsWith("HTTPS")){
                result = HttpSend.httpspost(url, "application/json", requ);
            }else {
                result = HttpSend.post(url, "application/json", requ);
            }
            handleResult(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private HashMap<String,Object> getRequestMap(){
        Long l = System.currentTimeMillis();
        HashMap<String,Object> request = new HashMap<String, Object>();
        request.put("APPAUTHTOKEN","APPAUTHTOKEN");
        request.put("APPID","APPID");
        request.put("ENCRYPTMETHOD","ENCRYPTMETHOD");
        request.put("SEQUENCEID","SEQUENC_"+l);
        request.put("SIGN","SIGN");
        request.put("SIGNMETHOD","SIGNMETHOD");
        request.put("REQDATA",getRequestData());
        return request;
    }


    abstract protected String getRequestUrl();

    abstract protected Map<String,Object> getRequestData();

    protected void handleResult(String result) throws UnsupportedEncodingException {System.out.println(result);};
}
